package edu.uic.ids517;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale.Category;

import javax.annotation.PostConstruct;
import javax.faces.model.SelectItem;

public class TableListBean implements Serializable {

	private String tableName;
	private List<String>  columnName;
	private List<String> tableNames; 
	private List<String> columnNames; 
	private int noOfColumns;

	public int getNoOfColumns() {
		return noOfColumns;
	}

	public void setNoOfColumns(int noOfColumns) {
		this.noOfColumns = noOfColumns;
	}


	public List<String> getColumnName() {
		return columnName;
	}


	public void setColumnName(List<String> columnName) {
		this.columnName = columnName;
	}


	ActionBeanDbaseAccess dbaseBean;



	public List<String> getColumnNames() {
		return columnNames;
	}


	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}


	public ActionBeanDbaseAccess getDbaseBean() {
		return dbaseBean;
	}


	public void setDbaseBean(ActionBeanDbaseAccess dbase) {
		this.dbaseBean = dbase;
	}


	public String getTableName() {
		return tableName;
	}


	public void setTableName(String tableName) {
		this.tableName = tableName;
	}


	public List<String> getTableNames() {
		return tableNames;
	}


	public void setTableNames(List<String> tableNames) {
		this.tableNames = tableNames;
	}


	public void populateTables() throws SQLException {
		tableNames = getAllTables();
	}

	public void populateColumns() throws SQLException {
		columnNames = getAllColumns();
	}

	public List<String> getAllTables() throws SQLException{

		List<String> items = new ArrayList<String>();

		// database work

		dbaseBean.openConnection();
		String sqlSelectQuery = "SHOW tables ";
		ResultSet tableRS = dbaseBean.executeSelectSQL(sqlSelectQuery);
		while(tableRS.next()){


			items.add(tableRS.getString("Tables_in_f14g49"));

		}
		//remove adminTables
		items.remove("logintable");
		items.remove("logtable");
		items.remove("variabletypetable");

		return items;
	}


	public List<String> getAllColumns() throws SQLException{
		System.out.println("INSISE GET all Columsn..........");

		List<String> columnList = new ArrayList<String>();

		// database work		   
		dbaseBean.openConnection();
		String sqlSelectQuery = "SELECT * FROM " + getTableName() + "";

		// Execute the Create query
		ResultSet results =	dbaseBean.executeSelectSQL(sqlSelectQuery);

		// Getting the column names
		ResultSetMetaData rsmd = null;
		rsmd = results.getMetaData();
		int columnCount = rsmd.getColumnCount();
		for (int i=1; i<=columnCount; i++) {

			columnList.add(rsmd.getColumnName(i));
		}

		System.out.println("END of  GET all Columsn..........");

		noOfColumns = columnList.size();
		return columnList;
	}

	public List<Double> getAllRowsForSelectedColumn(String column){
		System.out.println("INSISE getAllRowsForSelectedColumn..........");
		List<Double> columnData = new ArrayList<Double>();
		dbaseBean.openConnection();
		String sqlSelectQuery = "SELECT "+ column +" FROM " + getTableName() + "";
		System.out.println("QUERY TO GET THE DATA FROM SELECTED COLUMN..."+sqlSelectQuery);
		ResultSet results =	dbaseBean.executeSelectSQL(sqlSelectQuery);
		ResultSetMetaData rsmd = null;
		try {
			rsmd = results.getMetaData();


			String type = rsmd.getColumnTypeName(1);
			System.out.println("START while loop.........."+rsmd.getColumnTypeName(1));

			while(results.next())
			{
				if (type == "VARCHAR" || type == "CHAR") {	
					System.out.println("VARCHAR..........");
					continue;
				} else if(type == "INT" ){
					System.out.println("INTEGER..........");
					System.out.println("INTEGER.........."+column);
					System.out.println("INTEGER.........."+results.getInt(column));
					System.out.println("INTEGER.........."+(double)results.getInt(column));
					columnData.add((double)results.getInt(column));
				} else if(type == "DOUBLE" ){
					System.out.println("DOUBLE..........");
					columnData.add(results.getDouble(column));
				} else if(type == "FLOAT" ){
					System.out.println("FLOAT..........");
					columnData.add((double)results.getFloat(column));
				}	
			}

			/*for(int j =1; j<=rowCount; j++)
		{

			if (type == "VARCHAR" || type == "CHAR") {	
				System.out.println("VARCHAR..........");
				continue;
			} else if(type == "INT" ){
				System.out.println("INTEGER..........");
				columnData.add((double)results.getInt(j));
			} else if(type == "DOUBLE" ){
				System.out.println("DOUBLE..........");
				columnData.add(results.getDouble(j));
			} else if(type == "FLOAT" ){
				System.out.println("FLOAT..........");
				columnData.add((double)results.getFloat(j));
			}
			System.out.println("END while loop..........");	
		}*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("END GET all Columsn..........");
		return columnData;
	}

}




